var houseID = parseInt(GetUrlString("id")),type = parseInt(GetUrlString("type")),pageNum=1;


// 旧房详情
function getList(houseID){
  var token = localStorage.getItem("hpWrite_token");
  $.ajax({
      type:'GET',
      url:address+'/oldBuildings/get.do?format=true',
      data:{
        "id":houseID,
        "hpWrite_token":token
      },
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          var basedata = result.data.result;
          var d = new detail(basedata);
          $("#slide").html(d.detailSlideTPL());
          doDetailSlide();
          $("#price").html(d.priceTPL());
          $("#houseInfo").html(d.houseInfoTPL());
          $("#description").text(basedata.description)
          $("#call").html(d.callTPL());
          $("#address").text(basedata.province+basedata.city+basedata.district+(basedata.address || ""));
          d.detailMap();
          $("#near").html(d.nearTPL());
        }
      }
  })
};

function detail(data) {
  this.data = data;
}

// 价格
detail.prototype.priceTPL =function(){
  var html = '',data = this.data;
  if(data.type==1){
      html += '<span class="black">租金:</span><span class="orange">'+data.price+'</span><span class="orange">元<span class="m-lr5">&frasl;</span>月</span>'
  }
  else{
      html += '<span class="black">总价:</span><span class="orange">'+data.price+'元</span></span>'
  }
  return html;
}
//房屋基本信息
detail.prototype.houseInfoTPL = function() {
  var html = "",data = this.data;
  html+= '<ul class="flex1">'+
      '<li><label>房型</label>'+data.roomNum+'房'+data.hallNum+'厅</li>'+
      '<li><label>楼层</label>'+data.floors+'层</li>'+
      '<li><label>朝向</label>'+data.direction+'</li>'+
    '</ul>'+
    '<ul class="flex1">'+
      '<li><label>面积</label>'+data.area+'㎡</li>'+
      '<li><label>装修</label>'+data.decoration+'</li>'+
      '<li><label>年代</label>'+data.year+'年</li>'+
    '</ul>';
    return html;
}

//轮播图
detail.prototype.detailSlideTPL = function(){
  var html ='',data = this.data;
  html += '<ul class="slideBox">';
  $.each(data.imageList,function(index,info){
    html += '<li><img src="'+info.image+'&size=640x300"></li>';
  })
    html += '</ul>'+
    '<div class="tips p-lr10 white l-h24 abs b0 l0 w100p bg-white">'+
      '<span class="name">'+data.name+'</span>'+
      '<span class="item white abs r10 t0"><span class="current">1</span><span>&frasl;</span><span class="all">1</span></span>'+
    '</div>';
  return html;
}

//轮播图do
function doDetailSlide(){
  var slength = $('#slide').find('li').length,
      $item = $('#slide').find('.item'),
      current = 1;
  $item.find('.all').text(slength);
  var slider = Swipe(document.getElementById('slide'), {
    auto: 3000,
    callback:function(index) {
      current ++;
      current = current > slength ? 1 : current;
      $item.find('.current').text(current);
    }
  });
};

detail.prototype.nearTPL = function(){
  var html = '',data = this.data;
  if(data.type ==2){
    html += '<li class="b-gray" data-type="1"><a href="oldList-nearCommunity.html?type='+type+'&lat='+data.latitude+'&lng='+data.longitude+'"><i class="dib icon-community w16 m-r10">&nbsp;</i>附近小区价格</a></li>';
  }
  html += '<li data-type="2"><a href="oldList-nearHouse.html?type='+type+'&lat='+data.latitude+'&lng='+data.longitude+'"><i class="dib icon-money w18 m-r10">&nbsp;</i>附近同价位房源</a></li>';
  return html;
}

// 百度地图
detail.prototype.detailMap = function(){
  var data = this.data;
  var map = new BMap.Map("detailMap");
  var point = new BMap.Point(data.longitude,data.latitude);
  map.centerAndZoom(point,16);
  var marker = new BMap.Marker(point);  // 创建标注
  map.addOverlay(marker);              // 将标注添加到地图中
  var label = new BMap.Label(data.name,{offset:new BMap.Size(20,-10)});
  marker.setLabel(label);
};

detail.prototype.callTPL = function(){
  var data = this.data;
  var html = '<p>售楼电话</p>'+
  '<a href="tel:'+data.senderPhone+'">'+data.senderPhone+'</a>'+
  '<a href="tel:'+data.senderPhone+'" class="callBtn"></a>';
  return html;
}

$("#layoutMore").on("click",function(){
  var $this = $(this);
  if($this.hasClass("turnDown")){
    $this.removeClass("turnDown");
    $("#layout").addClass("trafficH");
  }else{
    $this.addClass("turnDown");
    $("#layout").removeClass("trafficH");
  }
});

function near(){
  this.type = type;
  this.latitude = GetUrlString("lat");
  this.longitude = GetUrlString("lng");
}

near.prototype.getCommunity = function(){
  $.ajax({
      type:'GET',
      url:address+'/oldBuildings/listRecently.do?format=true',
      data:{
        "type":type,
        "pageNum":pageNum,
        "latitude":this.latitude,
        "longitude":this.longitude
      },
      dataType:'json',
      success:function(result){
        if(result.status ==200 && result.data.list !=""){
          var basedata = result;
          $("#Community").html(communityTPL(basedata))
        }else{
          var html = '<div class="nodata">没有数据</div>';
          $("#Community").html(html);
        }
      }
  })
}

near.prototype.getNearHouse = function(){
  $.ajax({
      type:'GET',
      url:address+'/oldBuildings/listRecentlyPrice.do?format=true',
      data:{
        "type":type,
        "pageNum":pageNum,
        "latitude":this.latitude,
        "longitude":this.longitude
      },
      dataType:'json',
      success:function(result){
        if(result.status ==200 && result.data.list !=""){
          var basedata = result;
          $("#nearHouse").html(nearHouseTPL(basedata))
        }else{
          var html = '<div class="nodata">没有数据</div>';
          $("#nearHouse").html(html);
        }
      }
  })
}

// 附近小区
function communityTPL(data){
  var html = "",basedata = data.data.list,point = localStorage.getItem("point");
  if(point){
    point = point.split(",");
    var lat1 = Number(point[0]),lng1 = Number(point[1]);
  };
  $.each(basedata,function(index,info){
        var lat2 =info.latitude,lng2=info.longitude,
        distance = getDistance(lat1,lng1,lat2,lng2);
        distance = (distance/1000).toFixed(1);
        html += '<a class="list p5 b-b db" href="oldList-detail.html?id='+info.id+'&type='+type+'">'+
          '<dl>'+
            '<dt><img src="'+(info.image || noImg)+'" class="pic"></dt>'+
            '<dd>'+
              '<div class="name black l-h20"><span class="fr gray"><i class="icon-gps"></i>'+distance+'km</span>'+info.name+'</div>'+
              '<p class="black m-t5 gray l-h20">最新均价<span class="orange b m-l5">'+info.price+'元<span class="m-lr5">&frasl;</span>平</span></p>'+
              '<p class="gray m-t5 l-h20">房价走势';
              switch(info.trendtype){
                case 0:
                  html += '<span class="m-l5">持平</span>';
                  break;
                case 1:
                  html += '<i class="icon-rise w10 m-l5">&nbsp;</i><span class="m-l5 orange">+'+info.housePriceTrend+'</span>';
                  break;
                case 2:
                  html += '<i class="icon-decline w10 m-l5">&nbsp;</i><span class="m-l5 green">-'+info.housePriceTrend+'</span>';
              }
              
            html += '</p></dd>'+
          '</dl>'+
      '</a>';
    })
  return html;
}
// 附近同价位房源
function nearHouseTPL(result) {
  var html = "",basedata = result.data.list;
  $.each(basedata,function(index,info){
    var img = info.image ? info.image+'&size=180x150' : noImg ;
      html += '<a class="list p5 b-b db" href="oldList-detail.html?id='+info.id+'&type='+type+'">'+
          '<dl>'+
            '<dt><img src="'+img+'" class="pic"></dt>'+
            '<dd>'+
              '<div class="name black">'+info.name+'</div>'+
              '<p class="black m-t5 gray"><span class="fr orange b">'+info.price+'元</span>'+info.houseType+'&nbsp;'+info.area+'㎡</p>'+
              '<p class="gray m-t5">'+info.address+'</p>'+
              '<div class="label orange m-t5">';
              var label = info.label;
              for(var i=0;i<label.length;i++){
                html+='<span>'+label[i]+'</span>';
              }
              html += '</div>'+
            '</dd>'+
          '</dl>'+
      '</a>';
    })
  return html;
}
